package com.android.kotlin.util

import android.content.Context
import android.graphics.drawable.Drawable
import android.support.v4.content.ContextCompat
import android.text.Html
import android.text.Html.FROM_HTML_MODE_COMPACT
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.ImageView
import android.widget.TextView
import com.android.kotlin.R
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.text.SimpleDateFormat
import java.util.*

/**
 * author：  HyZhan
 * created： 2018/10/11 16:33
 * desc：    扩展方法
 */

fun TextView.str(): String {
    return this.text.toString()
}

// 关闭软键盘
fun View.hideKeyboard() {
    val imm: InputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(this.windowToken, 0)
}

// 显示软键盘
fun View.showKeyboard() {
    val imm: InputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    this.requestFocus()
    imm.showSoftInput(this, 0)
}

// 将 html 代码转化 为 string
// Android N（API level 24.）废弃了Html.fromHtml(String)
fun String.toHtml(): String {
    return if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
        Html.fromHtml(this, FROM_HTML_MODE_COMPACT).toString()
    } else {
        Html.fromHtml(this).toString()
    }
}

fun ImageView.loadUrl(context: Context, url: String) {

    val options = RequestOptions()
            .placeholder(R.color.white)
            .error(R.color.white)
            .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
            .override(150, 200)

    Glide.with(context)
            .load(url)
            .apply(options)
            .into(this)
}

fun ImageView.loadUrl2(context:Context, url: String?){
    val options = RequestOptions()
        .placeholder(R.color.white)
        .error(R.color.white)
        .diskCacheStrategy(DiskCacheStrategy.RESOURCE)

    Glide.with(context)
        .load(url)
        .listener(object :RequestListener<Drawable>{
            override fun onLoadFailed(
                e: GlideException?,
                model: Any?,
                target: Target<Drawable>?,
                isFirstResource: Boolean
            ): Boolean {
                return false
            }

            override fun onResourceReady(
                resource: Drawable?,
                model: Any?,
                target: Target<Drawable>?,
                dataSource: DataSource?,
                isFirstResource: Boolean
            ): Boolean {
                this@loadUrl2.scaleType=ImageView.ScaleType.FIT_XY;
                var sc:Float= resource?.intrinsicHeight?.toFloat()!!/resource?.intrinsicWidth?.toFloat()!!
                var imgHeight=this@loadUrl2.width*sc
                var lp:ViewGroup.LayoutParams=this@loadUrl2.layoutParams;
                lp.height=imgHeight.toInt()
                this@loadUrl2.layoutParams=lp
                return false
            }
        })
        .apply(options)
        .into(this)



}

// 统一处理  (IO -> Main)
fun <T> Observable<T>.execute(observer: Observer<T>) {
    this.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(observer)
}

// 统一处理  (IO -> Main)
fun <T> Observable<T>.addCaches(onNext:(t:T)->Unit ):Observable<T> {
    return this.subscribeOn(Schedulers.io())
        .observeOn(Schedulers.newThread())
        .doOnNext {  onNext }
}


// 统一处理  (IO -> Main)
fun <T> Observable<T>.rxMain():Observable<T> {
    return this.subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
//        .doOnNext { dataBeanBaseResult->
//            if(addCache){
//                Schedulers.io().createWorker().schedule {
//                    if (dataBeanBaseResult != null || !dataBeanBaseResult!!.results.isEmpty()) {
//                        CacheUtils.getInstance().put(cacheId(), dataBeanBaseResult)
//                    }
//                }
//            }
//        }
}


fun Date.format(pattern: String = "yyyy-MM-dd"): String {
    return SimpleDateFormat(pattern, Locale.getDefault()).format(this)
}

// R.color.xxxx  -> @ColorInt
fun Int.toColor(context: Context): Int{
    return ContextCompat.getColor(context, this)
}


